// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';
import ElementUI, { Message } from 'element-ui';
import 'element-ui/lib/theme-default/index.css';
import moment from 'moment'; // 日期处理
import 'assets/custom-theme/index.css'; // 换肤版本element-ui css
import NProgress from 'nprogress'; // Progress 进度条
import 'nprogress/nprogress.css';// Progress 进度条 样式
import 'normalize.css/normalize.css';// normalize.css 样式格式化
import 'assets/iconfont/iconfont'; // iconfont 具体图标见https://github.com/PanJiaChen/vue-element-admin/wiki
import * as filters from './utils/filters'; // 全局vue filter
import Multiselect from 'vue-multiselect';// 使用的一个多选框组件，element-ui的select不能满足所有需求
import 'vue-multiselect/dist/vue-multiselect.min.css';// 多选框组件css
import Sticky from 'components/Sticky'; // 粘性header组件
import IconSvg from 'components/Icon-svg';// svg 组件
import vueWaves from './utils/directive/waves';// 水波纹指令
import imgErr from './utils/directive/imgerr';// 图片加载失败
import errLog from 'store/errLog';// error log组件
// import './mock/index.js';  // 该项目所有请求使用mockjs模拟
moment.locale('zh-cn');

// register globally
Vue.component('multiselect', Multiselect);
Vue.component('Sticky', Sticky);
Vue.component('icon-svg', IconSvg);
Vue.use(ElementUI);
Vue.use(vueWaves);
Vue.use(imgErr);

// register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
});

// permissiom judge
// function hasPermission(roles, permissionRoles) {
//   if (roles.indexOf('admin') >= 0) return true; // admin权限 直接通过
//   if (!permissionRoles) return true;
//   return roles.some(role => permissionRoles.indexOf(role) >= 0)
// }

// register global progress.
// const whiteList = ['/login', '/authredirect', '/reset', '/sendpwd'];// 不重定向白名单
const blackList = ['/composition', '/mall'];// 不能直接进入黑名单，导航链接点击不可直接跳转
const needLogin = ['/personal', '/mall/trade'];// 需登录名单
router.beforeEach((to, from, next) => {
  const needLogins = needLogin.filter(v => {
    const reg = new RegExp(v)
    return reg.test(to.path)
  })
  NProgress.start(); // 开启Progress
  if (to.redirectedFrom && blackList.indexOf(to.redirectedFrom) !== -1) {
    next({ ...from });
    NProgress.done();
  } else if (needLogins.length > 0) { // 需要登录： to.redirectedFrom && needLogin.indexOf(to.redirectedFrom) !== -1
    if (store.getters.uid) {
      next()
    } else {
      Message({
        message: '请先登录',
        type: 'error',
        duration: 2 * 1000
      })
      next({ ...from });
      NProgress.done();
    }
  } else {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    next();
  }
});

router.afterEach(() => {
  NProgress.done(); // 结束Progress
});

Vue.config.productionTip = false;

// 生产环境错误日志
if (process.env === 'production') {
  Vue.config.errorHandler = function(err, vm) {
    console.log(err, window.location.href);
    errLog.pushLog({
      err,
      url: window.location.href,
      vm
    })
  };
}

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})


